﻿using UnityEngine;
using Photon.Pun;
using Photon.Realtime;
using ExitGames.Client.Photon;
using System.Collections.Generic;

public class NetWorkingMgr : MonoBehaviourPunCallbacks 
{
    public RoomUI roomUI;
    public GameObject gameStartUI;
    public ChessController chessController;
    public static ChessType chessType;
    public GameObject room;
    public StartUI startUI;
    void Start()
    {
        PhotonNetwork.ConnectUsingSettings();
    }

    public override void OnConnectedToMaster()
    {
        base.OnConnectedToMaster();
       
        Debug.LogError("Connected sever");
        PhotonNetwork.JoinLobby(TypedLobby.Default);
    }
    public override void OnJoinedLobby()
    {
        base.OnJoinedLobby();
        Debug.LogError("加入大厅");
        room.SetActive(true);
    }
    public override void OnJoinedRoom()
    {
        base.OnJoinedRoom();
        PhotonNetwork.NickName = roomUI.playerName.text;
        roomUI.gameObject.SetActive(false);
        GameObject game= PhotonNetwork.Instantiate("Player",Vector3.zero,Quaternion.identity,0);
        if (PhotonNetwork.CurrentRoom.PlayerCount==1)
        {
            game.GetComponent<PlayerInput>().chessType = ChessType.Write;
        }
        else
        {
            game.GetComponent<PlayerInput>().chessType = ChessType.Black;
            Hashtable table = new Hashtable();
            table.Add("步子", ChessType.Write);
            PhotonNetwork.CurrentRoom.SetCustomProperties(table);
        }

        Debug.LogError("OnJoinedRoom");
    }

    public override void OnJoinRoomFailed(short returnCode, string message)
    {
        base.OnJoinRoomFailed(returnCode, message);
        Debug.LogError("OnJoinRoomFailed="+returnCode+":"+message);
    }

    public override void OnCreatedRoom()
    {
        base.OnCreatedRoom();
        Debug.LogError("OnCreatedRoom");
        
    }

    public override void OnCreateRoomFailed(short returnCode, string message)
    {
        base.OnCreateRoomFailed(returnCode, message);
        Debug.LogError("OnCreateRoomFailed:" + returnCode + message);
    }

    public override void OnDisconnected(DisconnectCause cause)
    {
        base.OnDisconnected(cause);
        Debug.LogError("OnDisconnected:" + cause.ToString());
    }
    public override void OnMasterClientSwitched(Player newMasterClient)
    {
        base.OnMasterClientSwitched(newMasterClient);
        Debug.LogError("OnMasterClientSwitched" + newMasterClient.IsLocal);
    }
    public override void OnRoomPropertiesUpdate(Hashtable propertiesThatChanged)
    {
        base.OnRoomPropertiesUpdate(propertiesThatChanged);

    }
    public override void OnPlayerEnteredRoom(Player newPlayer)
    {
        base.OnPlayerEnteredRoom(newPlayer);
        Debug.LogError("OnPlayerEnteredRoom:" + newPlayer.ActorNumber);
        Debug.LogError(newPlayer.NickName + "进入房间");
        //Hashtable players = PhotonNetwork.CurrentRoom.CustomProperties;
        //if (players.ContainsKey("players"))
        //{
        //    List<string> playerNames = (List<string>)players["players"];
        //    playerNames.Add(newPlayer.NickName);
        //}
        //else
        //{
        //    players.Add("players",new List<string>() { newPlayer.NickName });
        //}
        //PhotonNetwork.CurrentRoom.SetCustomProperties(players);
        //startUI.UpdatePlayerList();
    }

    public override void OnPlayerLeftRoom(Player otherPlayer)
    {
        base.OnPlayerLeftRoom(otherPlayer);
        Debug.LogError("OnPlayerLeftRoom" + otherPlayer.ActorNumber);
        //Hashtable players = PhotonNetwork.CurrentRoom.CustomProperties;
        //if (players.ContainsKey("players"))
        //{
        //    List<string> playerNames = (List<string>)players["players"];
        //    playerNames.Remove(otherPlayer.NickName);
        //}
        //startUI.UpdatePlayerList();

    }
}